<?php
/*
; // Criscomp Informatika Kft - JP-Bookmark for Joomla! 1.5.x & Joomla 1.6.0 Beta 1
// Copyright (c) 2010 Criscomp Informaika Kft. All rights reserved.
// Released under the GNU/GPL license: http://www.gnu.org/copyleft/gpl.html
// GNU/GPL licenc alatt kiadott szoftver: http://www.gnu.hu/gpl.html
// More info at the official homepage : http://www.joomlaportal.hu/jp-bookmark/index.php
// Designed and developed by the programmers of JoomlaPortal.hu team
// *** Project Start @ April 26th, 2010 ***
// *** Project last updated @ May 25th, 2010  ***
// Bugs what find ourself or users feedback:
// Notice: Undefined index: HTTPS in ... -> fixed @ .30
// Notice: Undefined property: stdClass::$readmore_link in ... -> fixed @ .30
// IWIW icon not full transparent -> fixed @ .30
// digg.com added @ .31
// fixed some URL mistakes when joomla not runs from web server's document root @ .32
// fixed google bookmarks (labelling works perfectly) @ .32
// rewritten installation xml file and main program code, for Joomla 1.6 forward compatibility
// JP-Bookmark v1.6.x.3+  works with Joomla 1.6.0 Beta 1 [Hope]
// JP-Bookmark is now compatibe PHP4 & PHP5.2.x (PHP5.3 not tested)
// PHP/Joomla! versions tested -> [PHP4.4.4-8+etch6/J!1.5.17], [PHP5.2.6-1+lenny4/J!1.5.17], [PHP5.2.6-1+lenny8/J!1.6.0 Beta 1]
// Added Social Bookmarking position "only bottom of article" for user request
// Tested with Joomla 1.6.0 Beta 2 and works fine with it.
*/
defined( '_JEXEC' ) or die( 'Nono, a kutyafáját' );
   jimport( 'joomla.plugin.plugin' );


class plgContentjp_bookmark extends JPlugin {

 /* for PHP 4.x compatibility [some feedback from users]   */
        function plgContentjp_bookmark( &$subject, $params )
        {
                parent::__construct( $subject, $params );
        }


// Those are the main fixed parameters. We say to you "Thank You", if you don't disturb those variables
var $versionofbookmark='<!-- v1.6.x.5 -->';
var $hezag='style=\'padding:3px 0px 0px 4px;\'';
var $t_sz='JP-Bookmark';
var $h_sz="style='font-size:9px;display:inline;line-height:.8em;'";
var $urlem='plugins/content/';
var $url16='jp-bookmark-icons/';
var $plusszovegelej='<span style="text-align:center;margin-bottom: 3px;float:left;clear:both;">';
var $plusszovegelej_btm='<span style="text-align:center;margin-bottom: 3px;margin-left:2px;float:left;">';
var $k2_fix="<div style='clear:both;'></div>";


function tamogatas ($enged,$holaspan=0,$realarticle=0)
{
// running extra circle for joomla 1.6
$j6_utvonal=(substr(JVERSION,0,3)=='1.6')? $this->urlem.'jp_bookmark/'.$this->url16 : $this->urlem.$this->url16;
$utvonal=JURI::base( true ).'/'.$j6_utvonal;
if (($enged) && (JRequest::getCmd('view','')!='frontpage') && (JRequest::getInt('id','')==$realarticle)) {$document =& JFactory::getDocument();$document->addScript( $utvonal.'jp_bookmark.js' );$rndct_sp=rand(2001,4000);$kepem="<span style='float:left;display:inline;'><a href=\"http://webindex.hu/keresohtml?cx=partner-pub-8105889682979123:q35ogcef0n4&cof=FORID:10&ie=UTF-8&q=%22JP-Bookmark%22 %22Facebook Like%22 %22JoomlaPortal.hu%22\" target=\'_blank\'><img onmouseover=\"return mutat_jp_information('pr_inf".$rndct_sp."','')\" onMouseOut=\"return mutat_jp_information('','pr_inf".$rndct_sp."')\" ".$this->hezag." title=\"".$this->t_sz."\" alt=\"".$this->t_sz."\" src =\"".$utvonal."jp_bookmark.gif\" border=\"0\"/></a></span>";$tamogato_script=$kepem.'<div style="text-size:8px;display:none;float:right;" id="pr_inf'.$rndct_sp.'">';$tamogato_script.='<div '.$this->h_sz.' >'.$this->t_sz.' plugin'.$this->versionofbookmark.'<br />www.joomlaportal.hu</div></div>';}else{$tamogato_script='';}
//    http://www.google.com/search?q=%22JP-Bookmark%22 %22Facebook Like%22\
 return  $tamogato_script;
}

function sorrendbe_vissza($melyik_kell,$locationom,$title,$rndct,&$params)
{
 $mostezmegyvissza='';
 $delicious_tag=$this->params->get('delicious_tag', '');
 $j6_utvonal=(substr(JVERSION,0,3)=='1.6')? $this->urlem.'jp_bookmark/'.$this->url16 : $this->urlem.$this->url16;
$utvonal=JURI::base( true ).'/'.$j6_utvonal;
switch ($melyik_kell)
    {
    case 1:
    // IWIW.hu
         $mostezmegyvissza.="<script>function iwiwshare_click".$rndct."()  {window.open('http://iwiw.hu/pages/share/share.jsp?u='+encodeURIComponent('http://".$locationom."')+'&t='+encodeURIComponent('".$title."'),'iwiwshare','toolbar=0,status=0,location=1, width=650,height=600,scrollbars=1');return false;}</script><a rel=\"nofollow\" href=\"http://iwiw.hu/pages/share/share.jsp?u=<url>\" onclick=\"return iwiwshare_click".$rndct."()\" target=\"_blank\" ><img ".$this->hezag." src =\"".$utvonal."iwiw.gif\" alt=\"IWIW megosztás\" title=\"IWIW megosztás\" border=\"0\" /></a>";
        $iwiw_picture= $this->params->get('relevant_picture', '-1');
        if (($iwiw_picture=='-1') || ($iwiw_picture=='')){}else{
        $doc =& JFactory::getDocument();
        $doc->addCustomTag( '<link rel="image_src" href="http://'.$_SERVER["HTTP_HOST"].'/images/'.$iwiw_picture.'" />   ' );}

    break;
    case 2:
    // startlap.hu
    $mostezmegyvissza.="<a rel=\"nofollow\" href=\"http://www.startlap.hu/\" onclick=\"window.open('http://www.startlap.hu/sajat_linkek/addlink.php?url='+encodeURIComponent('".$locationom."')+'&tags=".$delicious_tag."&title='+encodeURIComponent('".$title."'));return false;\" title=\"Startlap kedvenchez\" ><img ".$this->hezag." alt=\"Startlap kedvenchez\" src =\"".$utvonal."startlap.gif\" border=\"0\"/></a>";
    break;
    case 3:
    // google.com
    $delicious_tag_g=mb_ereg_replace(' ',', ',$delicious_tag);
    $mostezmegyvissza.="<a rel=\"nofollow\" href=\"http://www.google.com/\" onclick=\"window.open('https://www.google.com/bookmarks/mark?op=add&bkmk='+encodeURIComponent('".$locationom."')+'&title='+encodeURIComponent('".$title."')+'&labels=".$delicious_tag_g."');return false;\" title=\"Google könyvjelző\" ><img ".$this->hezag." alt=\"Google könyvjelző\" src =\"".$utvonal."google.gif\" border=\"0\"/></a>";
    break;
    case 4:
    // delicious, the oldest one
   $mostezmegyvissza.="<a class=\"Delicious\" rel=\"nofollow\" href=\"http://del.icio.us/\" onclick=\"window.open('http://del.icio.us/post?v=2&url='+encodeURIComponent('".$locationom."')+'&tags=".$delicious_tag."&title='+encodeURIComponent('".$title."'));return false;\" title=\"Link megosztása: Del.icio.us\"><img ".$this->hezag." alt=\"Link megosztása: Del.icio.us\" src =\"".$utvonal."delicious.gif\" border=\"0\"/></a>";
    break;
    case 5:
    // TWITTER the twit site :)
    $szoveghossz=mb_strlen($title)+8+mb_strlen($locationom);
    $ujtitle=$title;
    if ($szoveghossz>=140){$maxtitlehossz=139-8-mb_strlen($locationom);$ujtitle=mb_substr($title,0,$maxtitlehossz);}
    $mostezmegyvissza.="<script>function twitshare_click".($rndct+1)."() {window.open('http://twitter.com/home?status='+encodeURIComponent('".$ujtitle." ')+encodeURIComponent('http://".$locationom."'),'twitshare','toolbar=0,status=0,location=1, width=650,height=600,scrollbars=1');return false;}</script><a rel=\"nofollow\" href=\"#\" onclick=\"return twitshare_click".($rndct+1)."()\" target=\"_blank\" ><img ".$this->hezag." src =\"".$utvonal."twitter.gif\" alt=\"Twitter\" title=\"Twitter\" border=\"0\" /></a>";
    break;
    case 6:
    // linktér
$mostezmegyvissza.="<script>function twitshare_click".($rndct+2)."() {window.open('http://www.linkter.hu/index.php?action=suggest_link&url='+encodeURIComponent('http://".$locationom."')+'&title='+encodeURIComponent('".mb_convert_encoding($title,'ISO-8859-2','UTF-8')." ')+'&tag=".$delicious_tag."','twitshare','toolbar=0,status=0,location=1, width=650,height=600,scrollbars=1');return false;}</script><a rel=\"nofollow\" href=\"#\" onclick=\"return twitshare_click".($rndct+2)."()\" target=\"_blank\" ><img ".$this->hezag." src =\"".$utvonal."linkter.gif\" alt=\"Ajánlás a linkter.hu-ra\" title=\"Ajánlás a linkter.hu-ra\" border=\"0\" /></a>";

    break;
    case 7:
    // VIPSTART
     $szoveghossz=mb_strlen($title);
$ujtitle=$title;
 if ($szoveghossz>=26){$ujtitle=mb_substr($title,0,26);}
$mostezmegyvissza.="<script>function twitshare_click".($rndct+3)."() {window.open('http://www.vipstart.hu/addurl.php?link='+encodeURIComponent('http://".$locationom."')+'&cim='+encodeURIComponent('".$ujtitle."'),'twitshare','toolbar=0,status=0,location=1, width=650,height=600,scrollbars=1');return false;}</script><a rel=\"nofollow\" href=\"#\" onclick=\"return twitshare_click".($rndct+3)."()\" target=\"_blank\" ><img ".$this->hezag." src =\"".$utvonal."vipstart.gif\" alt=\"Ajánlás a vipstart.hu-ra\" title=\"Ajánlás a vipstart.hu-ra\" border=\"0\" /></a>";
    break;
    case 8:
    // FaceBook, the most popular at this time :)
    $mostezmegyvissza.="<a rel=\"nofollow\" href=\"http://www.facebook.com/\" onclick=\"window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent('".$locationom."')+'&t='+encodeURIComponent('".$title."'));return false;\" title=\"Facebook\" ><img ".$this->hezag." alt=\"Facebook\" src =\"".$utvonal."facebook.gif\" border=\"0\"/></a>";
    break;
    case 9:
    $szoveghossz=mb_strlen($title);
$ujtitle=$title;
 if ($szoveghossz>=61){$ujtitle=mb_substr($title,0,60);}
    $mostezmegyvissza.="<script>function twitshare_click".($rndct+4)."() {window.open('http://digg.com/submit?phase=2&url='+encodeURIComponent('http://".$locationom."')+'&title='+encodeURIComponent('".$ujtitle." ')+'&type=0'+'','twitshare','toolbar=0,status=0,location=1, width=650,height=600,scrollbars=1');return false;}</script><a rel=\"nofollow\" href=\"#\" onclick=\"return twitshare_click".($rndct+4)."()\" target=\"_blank\" ><img ".$this->hezag." src =\"".$utvonal."digg.gif\" alt=\"Digg\" title=\"Digg\" border=\"0\" /></a>";
    break;
	case 10:
    $szoveghossz=mb_strlen($title);
$ujtitle=$title;
 if ($szoveghossz>=61){$ujtitle=mb_substr($title,0,60);}
  $delicious_tag_g=mb_ereg_replace(' ',', ',$delicious_tag);
    $mostezmegyvissza.="<script>function twitshare_click".($rndct+5)."() {window.open('http://urlguru.hu/ajanl.aspx?url='+encodeURIComponent('http://".$locationom."')+'&title='+encodeURIComponent('".$ujtitle." ')+'&opinion=".$title."'+'&rate=5'+'&tags=".$delicious_tag_g."'+'','twitshare','toolbar=0,status=0,location=1, width=800,height=600,scrollbars=1');return false;}</script><a rel=\"nofollow\" href=\"#\" onclick=\"return twitshare_click".($rndct+5)."()\" target=\"_blank\" ><img ".$this->hezag." src =\"".$utvonal."urlguru.gif\" alt=\"Ajánlom a Gurura\" title=\"Ajánlom a Gurura\" border=\"0\" /></a>";
    break;
   default:


    }
return $mostezmegyvissza;
}

function onBeforeDisplayContent( &$row, &$params, $limitstart,$fromwhere=0 )
        {
         global $mainframe;
         $muszalyigy='';
         $sorrend=$this->params->get('sorrend','0');
         $sorrend_tomb=(($sorrend=='') || ($sorrend=='0')) ? null : explode(",", $sorrend);

// Ha nincs titlemegjelenítés akkor nem rakjuk ki alá a social Bookmarkokat.
// Egyelőre nem tudunk jobb megoldást arra hogy esetleg a mod_placehere miatt egy arctice modulpozicióban jelenik meg, persze ez nem egy tökéletes megoldás de ezt találtuk ki.
if  ((JRequest::getCmd('option','')=='com_k2')&&(isset($row->featured_ordering))){
            if ($this->params->get('k2_social','0')=='1'){
// webmaster want to disable K2 internal social bookmarking and replace with ours much thank you for that :)
            $paramsi = &JComponentHelper::getParams( 'com_k2' );
            $paramsi->set('itemShareLinks','0');
            $row->readmore_link=$row->link;
            }else{return '';exit;}

            }else{
            if ($params->get('show_title')<>'1' )  {
      	    return '' ;
            exit;
         	} }

$fb_like_kell= $this->params->get('facebook_like','1');
$fb_like_hovais=$this->params->get('facebook_hova','bottom');

  $plusszoveg=$this->k2_fix.$this->plusszovegelej;

// Ha engedi a támogatást akkor azt lehetőség szerint olyan helyen helyezzük el ahol a legkevésbé zavaró. Ez pedig a Facebook Like mellet van, de ha nem enegedi a támogatást akkor ne fussunk felesleges köröket.

// echo $this->params->get('jp_bk','1');
if ($this->params->get('jp_bk','1')){
// legalább egy social bookmarking-ot engedélyezett

// $vanlegalabbegy=(($iwiw)||($startlap)||($google)||($delicious)||($twitter)||($linkter)||($vipstart)||($facebook)) ? 1 : 0   ;
/* why we need before? Don't know
$felulrerakjuk=(($fb_like_hovais=='bottom') && ($fb_like_kell)) ? 0 : 1;
*/
$vanlegalabbegy=(sizeof($sorrend_tomb)>=1)? 1 : 0;
$biztosfelulre= (($vanlegalabbegy) && (!$fb_like_kell)) ? 1 : 0;
}else{
 $biztosfelulre=0;
}
//  echo $vanlegalabbegy;
   $title=$row->title;
   $delicious_tag=$this->params->get('delicious_tag', '');
// $hezag='style=\'padding-left:4px;padding-top:3px;\'';
    $facebookup=(($fb_like_hovais=='top') && ($fb_like_kell=='1')) ? 1 : 0 ;

   $rndct=rand(10,2000);
      if (empty($row->readmore_link) || !isset($row->readmore_link)){
//   if ($row->readmore_link=='' || !isset($row->readmore_link)){
       $locationom=$_SERVER["HTTP_HOST"].JRoute::_('index.php?option=com_content&view=article&id='.$row->id);
   }else{      $locationom=$_SERVER["HTTP_HOST"].$row->readmore_link;}
$fbl='';
if ($facebookup && ((JRequest::getCmd( 'view' )=='article') || (JRequest::getCmd('option')=='com_k2')) ) {$fbl.= $this->fblike_generate_iframe($locationom,$params);}

// ha a felhasznló csak azt szeretné, hogy a cikk végén jelenjen meg a Social Bookmark akkor azt itt engedélyezzük vagy sem
switch ($fromwhere)
{
    case 0:
    if ($this->params->get('elej_veg','0')!='2'){
     $meret=sizeof($sorrend_tomb);
        // i don't know which is faster but http://www.phpbench.com/ says 'while' is faster than 'for'. The choice is yours.
        $namikell=0;while($namikell<$meret)
        //for ($namikell=0;$namikell<$meret;$namikell++)
        {
        $plusszoveg.=$this->sorrendbe_vissza($sorrend_tomb[$namikell],$locationom,$title,$rndct,$params);
        ++$namikell;
        }
        $muszalyigy=((($facebookup) || ($biztosfelulre)) && ($vanlegalabbegy)) ? $this->tamogatas($this->params->get('jp_bk','1'),0,$row->id) : '';
        }
        $fbl2=$fbl;

    break;
    case 1:
         $meret=sizeof($sorrend_tomb);
        // i don't know which is faster but http://www.phpbench.com/ says 'while' is faster than 'for'. The choice is yours.
        $namikell=0;while($namikell<$meret)
        //for ($namikell=0;$namikell<$meret;$namikell++)
        {
        $plusszoveg.=$this->sorrendbe_vissza($sorrend_tomb[$namikell],$locationom,$title,$rndct,$params);
        ++$namikell;
        }
        $fbl2='';
        $muszalyigy=((($facebookup) || ($biztosfelulre)) && ($vanlegalabbegy)) ? $this->tamogatas($this->params->get('jp_bk','1'),0,$row->id) : '';
    break;
}


$plusszoveg.="</span>".$muszalyigy.$fbl2.$this->k2_fix;

  return $plusszoveg;
        }

function onAfterDisplayContent( &$row, &$params, $limitstart,$titleba='0' )
        {
         global $mainframe;
         $tamogato_blink='';
         $j6_utvonal=(substr(JVERSION,0,3)=='1.6')? $this->urlem.'jp_bookmark/'.$this->url16 : $this->urlem.$this->url16;
         $utvonal=JURI::base( true ).'/'.$j6_utvonal;
         ;
  /* Nem a legszebb megoldás de ez van egyelőre, annak a kérésnek megfelelni ami azt kéri hogy lehessen a cikk elejére és végére is  bookmarkot rakni. Ismételt meghívás azaz mégegyszer lefut ami már egyszer lefutott :)
De azért ezt se futtassuk már le ha frontpagen vagyunk mert ott nem szokott hosszú lenni az introtext.
            */

         $tamogato_blink=((JRequest::getCmd('view','')!='frontpage')&&(($this->params->get('elej_veg','0')==1)||($this->params->get('elej_veg','0')==2)))? $this->onBeforeDisplayContent($row,$params,$limitstart,1):'';
//
      $fb_like_kell= $this->params->get('facebook_like','1');
      $fb_like_hovais=$this->params->get('facebook_hova','bottom');
      $kepem="<img ".$this->hezag." title=\"".$this->t_sz."\" alt=\"".$this->t_sz."\" src =\"".$utvonal."jp_bookmark.gif\" border=\"0\"/>";
      $tamogato_blink.=$this->tamogatas(($this->params->get('jp_bk','1'))&&($fb_like_kell)&& ($fb_like_hovais=='bottom'),0,$row->id) ;
          ;
//        echo '<pre>'.print_r($this->params).'</pre>';

// Csak akkor jelenítjük meg a Facebook tetszik-et ha egy direkt cikk van .
    switch (JRequest::getCmd('option',''))
        {
          case 'com_k2':
             if (!$fb_like_kell)
                            {
                             return $tamogato_blink;
                             exit;
                             }else{
                             $locationom=$_SERVER["HTTP_HOST"].$row->link;
                             }
          break;
          default:
          if ((JRequest::getVar( 'view' )!='article')|| (!$params->get('show_title')) || (!$fb_like_kell))  {

   	    return $tamogato_blink ;
        exit;
 	}else{$locationom=$_SERVER["HTTP_HOST"].$row->readmore_link;}

        }




   if  (($fb_like_kell=='1') && ($fb_like_hovais=='bottom')){

   $vissza=$this->fblike_generate_iframe($locationom,$params);
  return $tamogato_blink.$vissza;}
  else { return $tamogato_blink; }

        }
 /* */
// this function generate the FaceBook Like button with Iframe
function fblike_generate_iframe($fblike_url,&$params) {

$protocolextend=(!empty($_SERVER['HTTPS'])) ? 's' : '';
$kellarc=($this->params->get('arcok', 0)) ? "true" : "false";
         $holaspan=($this->params->get('facebook_hova','bottom')=='bottom')? $this->plusszovegelej_btm : $this->plusszovegelej;
	$jpfblikeframe = $holaspan.'<iframe src="http'.$protocolextend.'://www.facebook.com/plugins/like.php?href='.urlencode($fblike_url);
	$jpfblikeframe .= '&layout='.$this->params->get('facebook_style', 'standard');
    $jpfblikeframe .= '&show_faces='.$kellarc;
    $tenyleges_magassag=(($kellarc=="true")&&($this->params->get('magassag', '30')<90))? '90' : $this->params->get('magassag', '30');
//    $jpfblikeframe .= '&width='.$this->params->get('szelesseg', '100%');
    $jpfblikeframe .= '&height='.$tenyleges_magassag;
    $jpfblikeframe .= '&action='.$this->params->get('akcio', 'like');
    $jpfblikeframe .= '&font='.$this->params->get('betu', 'arial');
    $jpfblikeframe .= '&colorscheme='.$this->params->get('szinvilag', 'light').'" ';
	$jpfblikeframe .= ' id="jpfblikeframe" name="jpfblikeframe" ';
        $jpfblikeframe .= ' scrolling="no" frameborder="0" allowTransparency="true" ';
	$jpfblikeframe .= ' style="border:none; overflow:hidden; width:'.$this->params->get('szelesseg', '100').'%;height:'.$tenyleges_magassag.'px;display:inline;" ';
        $jpfblikeframe .= ' ></iframe></span>';
	return $jpfblikeframe;
}

// Joomla 1.6 related start
	function onContentBeforeDisplay($context, &$article, &$params, $limitstart)
	{
		$app = JFactory::getApplication();
		$urese='';
		$urese=$this->onBeforeDisplayContent($article,$params,$limitstart);
		return $urese;
	}

	function onContentAfterDisplay($context, &$article, &$params, $limitstart)
	{
		$app = JFactory::getApplication();
        $urese='';
		$urese=$this->onAfterDisplayContent($article,$params,$limitstart);
		return $urese;

	}
// Joomla 1.6 related ends

}

?>